<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回格式化字符串</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.soundex.html">« soundex</a></li>
      <li style="float: right;"><a href="function.sscanf.html">sscanf »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>返回格式化字符串</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sprintf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sprintf</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">sprintf</span> &mdash; <span class="dc-title">返回格式化字符串</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sprintf-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sprintf</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   返回一个根据格式化字符串 <code class="parameter">format</code> 生成的字符串。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sprintf-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    

 <dt>
<code class="parameter">format</code></dt>

 <dd>

  <p class="para">
   format 字符串通常由零或多个指令组成：普通字符（不包含
   <code class="literal">%</code>）——直接复制到结果，<em class="emphasis">转换规范</em>——获取每个参数的结果。
  </p>

  <p class="para">
   转换规范遵循此原型：
   <code class="literal">%[argnum$][flags][width][.precision]specifier</code>.
  </p>

  <p class="formalpara">
   <h5 class="title">Argnum</h5>
   <p class="para">
    整数后跟美元符号 <code class="literal">$</code>，用于指定转换中要处理的参数数量。
   </p>
  </p>
  
  <p class="para">
   <table class="doctable table">
    <caption><strong>标志</strong></caption>
    
     <thead>
      <tr>
       <th>标志</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">-</code></td>
       <td>
        在指定字段宽度时左对齐，默认右对齐
       </td>
      </tr>

      <tr>
       <td><code class="literal">+</code></td>
       <td>
        正数的加号 <code class="literal">+</code>前缀，默认负数的前缀是负号。
       </td>
      </tr>

      <tr>
       <td><code class="literal"> </code>(space)</td>
       <td>
        空格填充结果。这是默认设置。
       </td>
      </tr>

      <tr>
       <td><code class="literal">0</code></td>
       <td>
        仅用 0 进行左侧数字填充。使用 <code class="literal">s</code> 标志符可以右侧填充。
       </td>
      </tr>

      <tr>
       <td><code class="literal">&#039;</code>(char)</td>
       <td>
        用字符（char）填充结果。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <p class="formalpara">
   <h5 class="title">Width</h5>
   <p class="para">
    整数，表示此转换应（最少）产生多少个字符。
   </p>
  </p>

  <p class="formalpara">
   <h5 class="title">Precision</h5>
   <p class="para">
    小数点 <code class="literal">.</code> 后跟整数。标志符的含义如下：
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       <code class="literal">e</code>、<code class="literal">E</code>、<code class="literal">f</code>、<code class="literal">F</code>
       标志符：小数点后需要打印的位数（默认是 6）。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <code class="literal">g</code>、<code class="literal">G</code>、<code class="literal">h</code>、<code class="literal">H</code>
       标志符：这是要打印的最大有效位数。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       <code class="literal">s</code> 标志符：充当分界点，为字符串设置最大字符限制。
      </span>
     </li>
    </ul>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      如果小数点没有明确的精度值，则假设是 0。
     </span>
    </p></blockquote>
   </p>
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    尝试使用的位置标志符大于 <strong><code>PHP_INT_MAX</code></strong> 将会生成警告。
   </span>
  </p></blockquote>

  <p class="para">
   <table class="doctable table">
    <caption><strong>标志符</strong></caption>
    
     <thead>
      <tr>
       <th>标志符</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">%</code></td>
       <td>
        字面意思的百分号字符。不需要参数。
       </td>
      </tr>

      <tr>
       <td><code class="literal">b</code></td>
       <td>
        参数视为整数并以二进制数字呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">c</code></td>
       <td>
        参数视为整数并以 ASCII 字符呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">d</code></td>
       <td>
        参数视为整数并以（有符号）十进制数字呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">e</code></td>
       <td>
        参数当做科学符号处理（例如 1.2e+2）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">E</code></td>
       <td>
        与 <code class="literal">e</code> 标志符相同，但使用大写字母（例如 1.2E+2）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">f</code></td>
       <td>
        参数当做浮点数处理且作为浮点数呈现（locale aware）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">F</code></td>
       <td>
        参数当做浮点数处理且作为浮点数呈现（non-locale aware）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">g</code></td>
       <td>
        <p class="para">
         通用格式。
        </p>
        <p class="para">
         Let P equal the precision if nonzero, 6 if the precision is omitted,
         or 1 if the precision is zero.
         Then, if a conversion with style E would have an exponent of X:
        </p>
        <p class="para">
         If P &gt; X ≥ −4, the conversion is with style f and precision P − (X + 1).
         Otherwise, the conversion is with style e and precision P − 1.
        </p>
       </td>
      </tr>

      <tr>
       <td><code class="literal">G</code></td>
       <td>
        Like the <code class="literal">g</code> specifier but uses
        <code class="literal">E</code> and <code class="literal">f</code>.
       </td>
      </tr>

      <tr>
        <td><code class="literal">h</code></td>
        <td>
         Like the <code class="literal">g</code> specifier but uses <code class="literal">F</code>.
         Available as of PHP 8.0.0.
        </td>
       </tr>

       <tr>
        <td><code class="literal">H</code></td>
        <td>
         Like the <code class="literal">g</code> specifier but uses
         <code class="literal">E</code> and <code class="literal">F</code>. Available as of PHP 8.0.0.
        </td>
      </tr>

      <tr>
       <td><code class="literal">o</code></td>
       <td>
        参数视为整数并以八进制数字来呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">s</code></td>
       <td>
        参数视为字符串来呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">u</code></td>
       <td>
        参数视为整数并以无符号十进制数字呈现。
       </td>
      </tr>

      <tr>
       <td><code class="literal">x</code></td>
       <td>
        参数视为整数并作为十六进制数字呈现（带小写字母）。
       </td>
      </tr>

      <tr>
       <td><code class="literal">X</code></td>
       <td>
        参数视为整数并作为十六进制数字呈现（带大写字母）。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    <code class="literal">c</code> 类型标志符忽略填充和宽度
   </p>
  </div>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results
   </p>
  </div>

  <p class="para">
   变量将会强制转换为适合标志符的类型：
   <table class="doctable table">
    <caption><strong>类型处理</strong></caption>
    
     <thead>
      <tr>
       <th>类型</th>
       <th>标志符</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><span class="type">string</span></td>
       <td><code class="literal">s</code></td>
      </tr>

      <tr>
       <td><span class="type">int</span></td>
       <td>
        <code class="literal">d</code>,
        <code class="literal">u</code>,
        <code class="literal">c</code>,
        <code class="literal">o</code>,
        <code class="literal">x</code>,
        <code class="literal">X</code>,
        <code class="literal">b</code>
       </td>
      </tr>

      <tr>
       <td><span class="type">float</span></td>
       <td>
        <code class="literal">e</code>,
        <code class="literal">E</code>,
        <code class="literal">f</code>,
        <code class="literal">F</code>,
        <code class="literal">g</code>,
        <code class="literal">G</code>,
        <code class="literal">h</code>,
        <code class="literal">H</code>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </dd>



    
     <dt>
<code class="parameter">values</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sprintf-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个根据格式化字符串 <code class="parameter">format</code> 生成的字符串。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.sprintf-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       此函数失败时不再返回 <strong><code>false</code></strong> 。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.sprintf-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4811">
   <p><strong>示例 #1 参数替换</strong></p>
   <div class="example-contents"><p>
    支持按顺序用参数替换格式字符串里的占位符。
   </p></div>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$location&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'tree'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'There&nbsp;are&nbsp;%d&nbsp;monkeys&nbsp;in&nbsp;the&nbsp;%s'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
There are 5 monkeys in the tree
</pre></div>
   </div>
   <div class="example-contents"><p>
    假设，我们想把它国际化，在一个单独的文件中创建格式字符串，我们将它重写为：
   </p></div>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%s&nbsp;contains&nbsp;%d&nbsp;monkeys'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    我们现在有一个问题。
    格式字符串中占位符的顺序与代码中参数的顺序不匹配。
    我们希望保持代码原样，并在格式字符串中简单地指出占位符引用的参数。
    我们可以这样写格式化字符串：
   </p></div>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%2$s&nbsp;contains&nbsp;%1$d&nbsp;monkeys'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    另外一个好处是占位符可以重复使用，而无需在代码中添加更多参数。
   </p></div>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%2$s&nbsp;contains&nbsp;%1$d&nbsp;monkeys.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;That\'s&nbsp;a&nbsp;nice&nbsp;%2$s&nbsp;full&nbsp;of&nbsp;%1$d&nbsp;monkeys.'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    当使用参数替换时，<code class="literal">n$</code>
    <em class="emphasis">位置指示符</em> 必须紧跟在百分号（<code class="literal">%</code>）之后，在任何其他指示符之前，如下所示。
   </p></div>
  </div>

  <div class="example" id="example-4812">
   <p><strong>示例 #2 指定填充字符</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%'.9d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">123</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%'.09d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">123</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
......123
000000123
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4813">
   <p><strong>示例 #3 位置说明符与其他说明符</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The&nbsp;%2$s&nbsp;contains&nbsp;%1$04d&nbsp;monkeys'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
The tree contains 0005 monkeys
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4814">
   <p><strong>示例 #4 <span class="function"><strong>sprintf()</strong></span>: 零填充整数</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$isodate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%04d-%02d-%02d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <div class="example" id="example-4815">
   <p><strong>示例 #5 <span class="function"><strong>sprintf()</strong></span>: 货币格式</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$money1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">68.75</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">54.35</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$money1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$money2</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$money</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$formatted&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%01.2f"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$money</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$formatted</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
123.1
123.10
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4816">
   <p><strong>示例 #6 <span class="function"><strong>sprintf()</strong></span>: 科学记数法</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">362525200</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%.3e"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
3.625e+8
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sprintf-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.fprintf.html" class="function" rel="rdfs-seeAlso">fprintf()</a> - 将格式化后的字符串写入到流</span></li>
    <li class="member"><span class="function"><a href="function.vprintf.html" class="function" rel="rdfs-seeAlso">vprintf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.vsprintf.html" class="function" rel="rdfs-seeAlso">vsprintf()</a> - 返回格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.vfprintf.html" class="function" rel="rdfs-seeAlso">vfprintf()</a> - 将格式化字符串写入流</span></li>
    <li class="member"><span class="function"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
    <li class="member"><span class="function"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <li class="member"><span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
    <li class="member"><span class="function"><a href="function.date.html" class="function" rel="rdfs-seeAlso">date()</a> - 格式化 Unix 时间戳</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>